/**
 * Web HID device
 * 可以用于访问HID 设备，例如 键盘和游戏机。
 */

module.exports = (win) => {
  win.webContents.session.on('select-hid-device', (event, details, callback) => {
    event.preventDefault()
    console.info('select-hid-device', details)
    if (details.deviceList && details.deviceList.length > 0) {
      callback(details.deviceList[0].deviceId)
    }
  })

  win.webContents.session.on('hid-device-added', (event, device) => {    
    console.info('hid-device-added FIRED WITH', device)
  })

  win.webContents.session.on('hid-device-removed', (event, device) => {    
    console.info('hid-device-removed FIRED WITH', device)
  })

  win.webContents.session.setPermissionCheckHandler((webContents, permission, requestingOrigin, details) => {
    console.info('setPermissionCheckHandler', permission, details)
    if (permission === 'hid' && details.securityOrigin === 'file:///') {
      return true
    }
  })

  win.webContents.session.setDevicePermissionHandler((details) => {
    console.info('setDevicePermissionHandler', details)
    if (details.deviceType === 'hid' && details.origin === 'file://') {
      return true
    }
  })
}